//
//  CompanyReviewViewController.m
//  MyProject
//
//  Created by 石小强 on 2018/5/14.
//  Copyright © 2018年 yanjiaming. All rights reserved.
//

#import "CompanyReviewViewController.h"
#import "HCSStarRatingView.h"

@interface CompanyReviewViewController ()<UITextViewDelegate>

@property (nonatomic, strong) UIView *backV;
@property (nonatomic, strong) UIButton *evaluateBtn;
@property (nonatomic, strong) HCSStarRatingView *oneStarV;
@property (nonatomic, strong) HCSStarRatingView *twoStarV;
@property (nonatomic, strong) UITextView *contentTV;
@property (nonatomic, strong) UILabel *numL;

@end

@implementation CompanyReviewViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.navigationItem.title = @"公司点评";
    
    [self setupSubViews];
    
}

- (void)doBack:(id)sender {
    [self dismissViewControllerAnimated:YES completion:nil];
}

- (void)setupSubViews {
    UIView *backV = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight)];
    backV.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.4];
    self.backV = backV;
    
    CGFloat vW = kScreenWidth - 56;
//    CGFloat vH = 345 / 320 * vW;
    CGFloat vH = 345 * FitWidth;
    UIView *contentV = [[UIView alloc] initWithFrame:CGRectMake(28, 150 * FitWidth, vW, vH)];
    contentV.backgroundColor = [UIColor whiteColor];
    contentV.layer.cornerRadius = 10.0;
    contentV.layer.masksToBounds = YES;
    [backV addSubview:contentV];
    
    UIImageView *closeIV = [[UIImageView alloc] initWithFrame:CGRectMake(25, 20, 11, 11)];
    closeIV.image = [UIImage imageNamed:@"ic_Closes"];
    [contentV addSubview:closeIV];
    
    UIButton *closeB = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 50 * FitWidth, 47 * FitWidth)];
//    [closeB setBackgroundImage:[UIImage imageNamed:@"ic_Closes"] forState:UIControlStateNormal];
    [closeB addTarget:self action:@selector(backViewOnPress) forControlEvents:UIControlEventTouchUpInside];
    [contentV addSubview:closeB];
    
    UILabel *titleL = [[UILabel alloc] initWithFrame:CGRectMake(50, 0, vW - 100, 47 * FitWidth)];
    titleL.text = @"公司点评";
    titleL.textColor = UIColorFromRGB(0x48565A);
    titleL.textAlignment = NSTextAlignmentCenter;
    titleL.font = [UIFont fontWithName:@"PingFangSC-Regular" size:16];
    [contentV addSubview:titleL];
    
    UILabel *line = [[UILabel alloc] initWithFrame:CGRectMake(60, 47 * FitWidth, vW - 120, 1)];
    line.backgroundColor = kStyleColor;
    [contentV addSubview:line];
    
    UILabel *oneL = [[UILabel alloc] initWithFrame:CGRectMake(40 * FitWidth, 72 * FitWidth, 60, 14)];
    oneL.text = @"综合评分";
    oneL.textColor = UIColorFromRGB(0x48565A);
    oneL.font = [UIFont fontWithName:@"PingFangSC-Light" size:14];
    [contentV addSubview:oneL];
    
    HCSStarRatingView *oneStarV = [[HCSStarRatingView alloc] initWithFrame:CGRectMake(CGRectGetMaxX(oneL.frame) + 15, CGRectGetMinY(oneL.frame) - 3, vW - 80 * FitWidth - 75, 20)];
    oneStarV.emptyStarColor = UIColorFromRGB(0xBFBFBF);
    oneStarV.emptyStarImage = [UIImage imageNamed:@"ic_stars_没选中没选中Unselected"];
    oneStarV.filledStarImage = [UIImage imageNamed:@"ic_stears_Selected"];
    oneStarV.halfStarImage = [UIImage imageNamed:@"ic_stars_Halfselected"];
    oneStarV.maximumValue = 5;
    oneStarV.minimumValue = 0;
    oneStarV.value = 0;
    oneStarV.allowsHalfStars = YES;
//    oneStarV.accurateHalfStars = YES;
//    oneStarV.continuous = YES;
    [contentV addSubview:oneStarV];
    
    UILabel *twoL = [[UILabel alloc] initWithFrame:CGRectMake(40 * FitWidth, 112 * FitWidth, 60, 14)];
    twoL.text = @"工作压力";
    twoL.textColor = UIColorFromRGB(0x48565A);
    twoL.font = [UIFont fontWithName:@"PingFangSC-Light" size:14];
    [contentV addSubview:twoL];
    
    HCSStarRatingView *twoStarV = [[HCSStarRatingView alloc] initWithFrame:CGRectMake(CGRectGetMaxX(twoL.frame) + 15, CGRectGetMinY(twoL.frame) - 3, vW - 80 * FitWidth - 75, 20)];
    twoStarV.emptyStarColor = UIColorFromRGB(0xBFBFBF);
    twoStarV.emptyStarImage = [UIImage imageNamed:@"ic_stars_没选中没选中Unselected"];
    twoStarV.filledStarImage = [UIImage imageNamed:@"ic_stears_Selected"];
    twoStarV.halfStarImage = [UIImage imageNamed:@"ic_stars_Halfselected"];
    twoStarV.maximumValue = 5;
    twoStarV.minimumValue = 0;
    twoStarV.value = 0;
    twoStarV.allowsHalfStars = YES;
//    twoStarV.accurateHalfStars = YES;
//    twoStarV.continuous = YES;
    [contentV addSubview:twoStarV];
    
    UITextView *contentTV = [[UITextView alloc] initWithFrame:CGRectMake(25, 148 * FitWidth, vW - 50, 96 * FitWidth)];
    contentTV.layer.cornerRadius = 5.0f;
    contentTV.layer.masksToBounds = YES;
    contentTV.layer.borderWidth = 1.0f;
    contentTV.layer.borderColor = [kStyleColor CGColor];
    contentTV.delegate = self;
    self.contentTV = contentTV;
    [contentV addSubview:contentTV];
    
    
    UILabel *numL = [[UILabel alloc] initWithFrame:CGRectMake(vW - 80, 244* FitWidth - 15, 50, 12)];
    numL.text = @"0/200";
    numL.textColor = UIColorFromRGB(0xA5B9C2);
    numL.font = [UIFont fontWithName:@"PingFangSC-Light" size:12];
    self.numL = numL;
    [contentV addSubview:numL];
    
    UIButton *evaluateBtn = [[UIButton alloc] initWithFrame:CGRectMake(vW - 78 - 25 * FitWidth, CGRectGetMaxY(contentTV.frame), 78, 33 * FitWidth)];
    [evaluateBtn setTitle:@"匿名评价" forState:UIControlStateNormal];
    [evaluateBtn setTitleColor:UIColorFromRGB(0xA5B9C2) forState:UIControlStateNormal];
    evaluateBtn.titleLabel.font = [UIFont fontWithName:@"PingFangSC-Light" size:11];
    [evaluateBtn setImage:[UIImage imageNamed:@"ic_Decrypt_Unselected"] forState:UIControlStateNormal];
    [evaluateBtn setImage:[UIImage imageNamed:@"ic_Decrypt_Selected"] forState:UIControlStateSelected];
    [evaluateBtn setTitleEdgeInsets:UIEdgeInsetsMake(0, 10, 0, 0)];
    [evaluateBtn addTarget:self action:@selector(evaluateBtnClick) forControlEvents:UIControlEventTouchUpInside];
    self.evaluateBtn = evaluateBtn;
    [contentV addSubview:evaluateBtn];
    
    
    UIButton *okBtn = [[UIButton alloc] initWithFrame:CGRectMake(25 * FitWidth, CGRectGetMaxY(evaluateBtn.frame), vW - 50, 49 * FitWidth)];
    [okBtn setTitle:@"确定" forState:UIControlStateNormal];
    [okBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    okBtn.titleLabel.font = [UIFont fontWithName:@"PingFangSC-Regular" size:17];
    [okBtn setBackgroundColor:UIColorFromRGB(0x42CDD9)];
    [okBtn addTarget:self action:@selector(okBtnClick) forControlEvents:UIControlEventTouchUpInside];
    okBtn.layer.cornerRadius = 10.0f;
    okBtn.layer.masksToBounds = YES;
    [contentV addSubview:okBtn];
    
    
    [[UIApplication sharedApplication].keyWindow addSubview:backV];
}

- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text {
    NSString *result = [NSString stringWithFormat:@"%@%@", textView.text, text];
    if (result.length > 200) {
        [CommonUtils showToast:@"最多输入200个字符"];
        return NO;
    }
    return YES;
}

- (void)textViewDidChange:(UITextView *)textView {
    self.numL.text = [NSString stringWithFormat:@"%zd/200", textView.text.length];
}

- (void)okBtnClick {
    
    WS(weakSelf)
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    params[@"companyId"] = self.companyId;
    params[@"content"] = self.contentTV.text;
    params[@"isAnonymous"] = self.evaluateBtn.selected ? @"1" : @"0";
    NSMutableArray *scores = [NSMutableArray array];
    NSMutableDictionary *scoreOne = [NSMutableDictionary dictionary];
    scoreOne[@"code"] = @"COMPREHENSIVE_SCORE";
    scoreOne[@"score"] = [NSString stringWithFormat:@"%.1f", self.oneStarV.value];
    [scores addObject:scoreOne];
    
    NSMutableDictionary *scoreTwo = [NSMutableDictionary dictionary];
    scoreTwo[@"code"] = @"RECOMMEND_SCORE";
    scoreTwo[@"score"] = [NSString stringWithFormat:@"%.1f", self.twoStarV.value];
    [scores addObject:scoreTwo];
    
    params[@"scores"] = scores;
    
    [SendRequest submitCompanyScoreWithParam:params result:^(NSDictionary *result, NSError *error) {
        result = [CommonUtils decryptDicWithResult:result];
        if([result[@"result"] integerValue] == 1) {
            [weakSelf backViewOnPress];
        } else {
            [CommonUtils showToast:[result objectForKey:@"msg"]];
        }
    }];
}

- (void)evaluateBtnClick {
    self.evaluateBtn.selected = !self.evaluateBtn.selected;
}

- (void)backViewOnPress {
    [self.backV removeFromSuperview];
    [self dismissViewControllerAnimated:NO completion:nil];
    [[NSNotificationCenter defaultCenter]postNotificationName:@"endAuthentication" object:nil];
}


@end
